package com.superhelper.superhelper_web.controller.setting;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.superhelper.superhelper_web.common.CommonHelper;
import com.superhelper.superhelper_web.controller.base.AbstractController;
import com.superhelper.superhelper_web.domain.Admin;
import com.superhelper.superhelper_web.domain.PageModel;
import com.superhelper.superhelper_web.domain.Resource;
import com.superhelper.superhelper_web.domain.Resp;
import com.superhelper.superhelper_web.domain.TreeNode;

/**
 * 
 * @author xiejunbo
 *
 */
@Controller
@RequestMapping("admin/resource")
public class ResourceController extends AbstractController {

	/**
	 * 查看资源
	 * 
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView getResourceById(ModelMap model, PageModel pageModel) {
		List<Resource> list = adminService.getResource(pageModel);
		ModelAndView mv = new ModelAndView("admin/setting/resources");
		mv.addObject("list", list);
		return mv;
	}

	/**
	 * 菜单选择页面
	 * 
	 * @return
	 */
	@RequestMapping("/setMenu")
	public ModelAndView setMenu(Integer userId, ModelMap model) {
		ModelAndView mv = new ModelAndView("admin/setting/radioTree");
		return mv;
	}

	/**
	 * 资源权限分配页面
	 * 
	 * @return
	 */
	@RequestMapping("/setPrivilege")
	public ModelAndView setPrivilege(@RequestParam("id") Integer userId,
			ModelMap model) {
		ModelAndView mv = new ModelAndView("admin/setting/checkBoxTree");
		mv.addObject("userId", userId);
		return mv;
	}


	/**
	 * 资源（tree）
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/tree")
	public Resp tree() {
		Admin admin = CommonHelper.getSessionUser();// 从session中获取admin对象
		List<TreeNode> trees = adminService.getTreeByUserId(admin.getId());
		return success(trees);
	}

	/**
	 * 编辑角色（TREE）
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/editTree")
	public Resp editTree(@RequestParam("roleId") Integer roleId) {
		Admin admin = CommonHelper.getSessionUser();// 从session中获取admin对象
		List<TreeNode> trees = adminService.getTreeByUserId(admin.getId());
		List<Resource> resources = adminService.getResourceByRoleId(roleId);
		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
		for (Resource r : resources) {
			map.put(r.getId(), r.getId());
		}

		for (TreeNode t : trees) {
			if (map.containsKey(t.getId())) {
				t.setChecked(true);
			}
		}
		return success(trees);
	}

	@RequestMapping("/add")
	public ModelAndView add() {
		ModelAndView mv = new ModelAndView("admin/setting/addResource");
		return mv;
	}

	@RequestMapping("/edit")
	public ModelAndView edit(Integer resourceId) {
		ModelAndView mv = new ModelAndView("admin/setting/editResource");
		Resource resource = adminService.getResourceById(resourceId);
		Resource parent = adminService.getResourceById(resource.getParentId());
		mv.addObject("resource", resource);
		mv.addObject("parent", parent);
		return mv;
	}

	/**
	 * 添加资源
	 * 
	 * @param dialect
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public Resp save(Resource resource) {
		try {
			int dialectId = adminService.saveResource(resource);
			if (dialectId > 0) {
				return success("添加成功！");
			} else {
				return fail("添加失败！");
			}
		} catch (Exception e) {
			log.error("save异常：{}", e);
			return fail("系统繁忙！");
		}

	}

	/**
	 * 更新资源
	 * 
	 * @param dialect
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Resp update(Resource resource) {
		try {
			int dialectId = adminService.updateResource(resource);
			if (dialectId > 0) {
				return success("更新成功！");
			} else {
				return fail("更新失败！");
			}
		} catch (Exception e) {
			log.error("update异常：{}", e);
			return fail("系统繁忙！");
		}

	}

	/**
	 * 删除资源
	 * 
	 * @param delete
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Resp delete(@RequestParam("resourceId") Long resourceId) {
		try {
			int dialectId = adminService.deleteResource(resourceId);
			if (dialectId > 0) {
				return success("删除成功！");
			} else {
				return fail("删除失败！");
			}
		} catch (Exception e) {
			log.error("delete异常：{}", e);
			return fail("系统繁忙！");
		}

	}

}
